package fr.irit.etracetool.etrace.util;

import java.util.HashMap;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.zest.core.widgets.Graph;
import org.eclipse.zest.core.widgets.GraphConnection;
import org.eclipse.zest.core.widgets.GraphNode;
import org.eclipse.zest.layouts.LayoutStyles;
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;

import fr.irit.etracetool.eobjectgraph.BipartiteGraph;
import fr.irit.etracetool.eobjectgraph.Node;
import fr.irit.etracetool.etrace.ETrace;
import fr.irit.etracetool.etrace.Link;

public class SWTTraceVisualisation {

	private Graph graph;
	
	private Shell shell;
	
	public SWTTraceVisualisation(BipartiteGraph bg) {
		BipartiteGraph struct;
		struct = bg;
		Display d = new Display();
		shell = new Shell(d);
		shell.setText("Transitive Graph Visualisation");
		shell.setLayout(new FillLayout());
		shell.setSize(1024, 768);

		graph = new Graph(shell,SWT.NONE);
		
		HashMap<Node, GraphNode> hm = new HashMap<Node, GraphNode>();
		for (Node n : struct.getTargets()) {
			GraphNode node;
			if (n.getElement() instanceof org.eclipse.uml2.uml.NamedElement) {
				node = new GraphNode(graph, SWT.NONE, ((org.eclipse.uml2.uml.NamedElement)n.getElement()).getName());
			}
			else if (n.getElement() instanceof com.googlecode.erca.NamedElement) {
				node = new GraphNode(graph, SWT.NONE, ((com.googlecode.erca.NamedElement)n.getElement()).getName());
			}
			else {
				node = new GraphNode(graph, SWT.NONE, (n.getElement()).toString());
			}
			node.setBackgroundColor(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_BLUE));
			node.setForegroundColor(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
			hm.put(n, node);
		}
		for (Node n : struct.getSources()) {
			//GraphNode node = new GraphNode(graph, SWT.NONE, ((NamedElement)n.getElement()).getName());
			GraphNode node;
			if (n.getElement() instanceof org.eclipse.uml2.uml.NamedElement) {
				node = new GraphNode(graph, SWT.NONE, ((org.eclipse.uml2.uml.NamedElement)n.getElement()).getName());
			}
			else if (n.getElement() instanceof com.googlecode.erca.NamedElement) {
				node = new GraphNode(graph, SWT.NONE, ((com.googlecode.erca.NamedElement)n.getElement()).getName());
			}
			else {
				node = new GraphNode(graph, SWT.NONE, (n.getElement()).toString());
			}
			node.setBackgroundColor(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GREEN));
			node.setForegroundColor(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
			hm.put(n, node);
			for (Node succ : n.getSuccessor()) {
				GraphConnection gc = new GraphConnection(graph, SWT.NONE, node, hm.get(succ));
				gc.setLineColor(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
			}
		}
		graph.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
		shell.open();
		while (!shell.isDisposed()) {
			while (!d.readAndDispatch()) {
				d.sleep();
			}
		}
	}
	
	public SWTTraceVisualisation(ETrace t) {
		Display d = new Display();
		shell = new Shell(d);
		shell.setText("Transitive Graph Visualisation");
		shell.setLayout(new FillLayout());
		shell.setSize(1024, 768);

		graph = new Graph(shell,SWT.NONE);
		
		HashMap<EObject, GraphNode> hm = new HashMap<EObject, GraphNode>();
		for (Link l: t.getAllLinks()) {
			EObject source = l.getSource();
			EObject target = l.getTarget();
			
			GraphNode gsource = hm.get(source);
			GraphNode gtarget = hm.get(target);
		}
	}
}
